﻿// -----------------------------------------------------------------------
// <copyright file="Form_Main.cs" company="ANGIE.D">
//     Copyright (c) 2012 ANGIE.D. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MyFileStore
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;
    using MyFileStore.FileStore;
    using System.IO;

    public partial class Form_Main : Form
    {
        public Form_Main()
        {
            this.InitializeComponent();
        }

        FileStore.FileStore fs = new FileStore.FileStore();

        private void RefreshMyFileStores()
        {
            TreeView_MyFileStores.Nodes.Clear();

            if (!Directory.Exists(Config.FSXMLFolder))
            {
                Directory.CreateDirectory(Config.FSXMLFolder);
            }

            DirectoryInfo directoryInfo = new DirectoryInfo(Config.FSXMLFolder);
            foreach (var file in directoryInfo.GetFiles("*" + Config.FSXMLFileSuffix))
            {
                string nodeName = file.Name.TrimEnd(Config.FSXMLFileSuffix.ToCharArray());
                TreeNode node = new TreeNode(string.Format("{0} ({1})", nodeName, file.LastWriteTime));
                node.Tag = nodeName;

                TreeView_MyFileStores.Nodes.Add(node);
            }
        }

        private void Form_Main_Load(object sender, EventArgs e)
        {
            RefreshMyFileStores();
        }

        private void TreeView_MyFileStores_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (TreeView_MyFileStores.SelectedNode != null)
            {
                fs = FileStore.FileStore.Load(TreeView_MyFileStores.SelectedNode.Tag.ToString());

                if (fs != null)
                {
                    this.TreeView_FullView.Nodes.Clear();
                    this.TreeView_FullView.Nodes.Add(FileStore.FileStore.CreateTreeViewNode(fs.OfflineInformation));
                }
            }
        }

        private void ToolStripButton_Main_NewFileStore_Click(object sender, EventArgs e)
        {
            SaveFileDialog_NewFileStore.InitialDirectory = Config.FSXMLFolder;
            SaveFileDialog_NewFileStore.ShowDialog();

            fs = new FileStore.FileStore();
            fs.Name = new FileInfo(SaveFileDialog_NewFileStore.FileName).Name.TrimEnd(Config.FSXMLFileSuffix.ToCharArray());

            ToolStripButton_Main_EditSelectedFileStore_Click(null, null);
            ToolStripButton_Main_RefreshSelectedFileStore_Click(null, null);
        }

        private void ToolStripButton_Main_EditSelectedFileStore_Click(object sender, EventArgs e)
        {
            if (fs != null)
            {
                FolderBrowserDialog_EditFileStore.ShowDialog();
                fs.DirectoryPath = FolderBrowserDialog_EditFileStore.SelectedPath;
            }
        }

        private void ToolStripButton_Main_RefreshSelectedFileStore_Click(object sender, EventArgs e)
        {
            if (fs != null && TreeView_MyFileStores.SelectedNode != null)
            {
                if (fs.TryToRefreshOfflineInformation())
                {
                    fs.Save();

                    TreeView_MyFileStores_AfterSelect(null, null);

                    int selectedIndex = TreeView_MyFileStores.SelectedNode.Index;
                    RefreshMyFileStores();

                    if (selectedIndex <= TreeView_MyFileStores.Nodes.Count - 1)
                    {
                        TreeView_MyFileStores.SelectedNode = TreeView_MyFileStores.Nodes[selectedIndex];
                    }
                }
                else
                {
                    MessageBox.Show(string.Format("'{0}' is NOT available...", fs.DirectoryPath), "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
